from flask import Flask, Blueprint, redirect
from flask import render_template
from flask import request
from flask import flash
import pymysql
import traceback

# 传递根目录
wqh_app = Blueprint('wqh_app', __name__)


# 默认路径访问注册页面
@wqh_app.route('/wqh_regist', methods=['GET', 'POST'])  # /regist
def regist():
    return render_template('register.html')

@wqh_app.route('/wqh_firmregist', methods=['GET', 'POST'])  # /regist
def firmregist():
    return render_template('firmregister.html')


# 函数注释：获取数据库连接
# 函数概述：通过调用pymysql包连接数据库
# 参数说明：
def get_conn():
    conn = pymysql.connect(
        host='139.217.107.173',
        port=3306,
        user='root',
        password='123456',
        database='project_training',
        charset='utf8'
    )
    cursor = conn.cursor()
    # 返回conn和cursor
    return conn, cursor


# 资源释放
def my_close(conn, cursor):
    conn.close()
    cursor.close()


# 获取登录参数及处理（个人）
@wqh_app.route('/registuser', methods=['get', 'post'])
def regestuser():
    conn, cursor = get_conn()
    # SQL 查询语句
    ssql = "select password from userinfo where username = %s"
    user = request.values.get('user')
    content=[user]
    #执行sql语句
    cursor.execute(ssql, content)
    # 获取执行结果 fetch_one()，判断结果
    res = cursor.fetchone()
    # 如果res返回None 表示没有找到数据，不存在可注册，存在注册失败
    if res is not None:
        flash('用户名已存在，注册失败')
    else:
        #print('该用户名可以使用')
        # 注册-> 插入数据，手动commit
        insert_sql = 'insert into userinfo (username, password,isadminan) values (%s,%s,"员工")'
        pwd = request.values.get('password')
        repwd = request.values.get('repassword')
        if(pwd == repwd):
            insert_params = [user, pwd]
            cursor.execute(insert_sql, insert_params)
            conn.commit()
            flash('注册成功')
            my_close(conn, cursor)
            return render_template('homepage.html')
        else:
            flash('两次输入密码不一致，注册失败')

    my_close(conn, cursor)

    return render_template('register.html')

####################企业######################
@wqh_app.route('/firmregistuser', methods=['get', 'post'])
def firmregestuser():
    conn, cursor = get_conn()

    #验证
    capt = request.values.get('captcha')
    if capt != '6657':
        flash('验证码错误，注册失败')
    else:
        # SQL 查询语句
        ssql = "select password from userinfo where username = %s"
        user = request.values.get('user')
        content = [user]
        # 执行sql语句
        cursor.execute(ssql, content)
        # 获取执行结果 fetch_one()，判断结果
        res = cursor.fetchone()
        # 如果res返回None 表示没有找到数据，不存在可注册，存在注册失败
        if res is not None:
            flash('用户名已存在，注册失败')
        else:
            #flash('该用户名可以使用')
            # 注册-> 插入数据，手动commit
            insert_sql = 'insert into userinfo (username, password,isadminan) values (%s,%s,"企业")'
            pwd = request.values.get('password')
            repwd = request.values.get('repassword')
            if (pwd == repwd):
                insert_params = [user, pwd]
                cursor.execute(insert_sql, insert_params)
                conn.commit()
                flash('注册成功')
                my_close(conn, cursor)
                return render_template('homepage.html')
            else:
                flash('两次输入密码不一致，注册失败')


    my_close(conn, cursor)

    return render_template('firmregister.html')